/* Copyright Andy Mitchell 2007. No warranties */

var GTDDEBUGMODE = true;
var GTDDEBUGMODE_DB = false;

function GTDInboxBrowser(){

try{
	var browser_appcontent = window.document.getElementById("appcontent");
	this.db = new GTDInboxDB("gtdinbox.sqlite");
	this.options = new FFOptions();
	
	this.stringBundle = document.getElementById("string-bundle");
	this._locale = this.stringBundle.getString("localename");

	if( this.options.getOption("char", "gtdinbox.debugMode")=="1" ){
		GTDDEBUGMODE = true;
	}else{
		GTDDEBUGMODE = false;
	}
		
	
	// Setup event handlers
	var fNewPage = this.createFNewPage();
	browser_appcontent.addEventListener("DOMContentLoaded", fNewPage, false);
	
	// Tab Events
	var tabContainer = gBrowser.tabContainer;
	var fTabClose = function(event){
			var currentBrowser = event.target.linkedBrowser;
			if( currentBrowser ){	// useful to block iframes
				var tabGTD = currentBrowser.gtdinboxPage;
				if( tabGTD ) tabGTD.unload();
			}
		};
	tabContainer.addEventListener("TabClose", GIPROTOTYPE.bind(this, fTabClose), false);

}catch(e){
	if( GTDDEBUGMODE ) alert("GTDInboxBrowser constructor\n" + e.toString());
}
}

GTDInboxBrowser.prototype.getLocalHtmFile = function(filename){
	//return "chrome://gtdinbox/content/locale/" + this._locale + "/" + filename + ".htm";
	return "chrome://gtdinbox/locale/" + filename + ".htm";
};



GTDInboxBrowser.prototype.createFNewPage = function(){
	var gtdinboxBrowser = this;
	var fNewPage = function(event){
			var currentBrowser = gBrowser.getBrowserForDocument(event.originalTarget);
			if( currentBrowser ){	// useful to block iframes
				try{
				var tabGTD = currentBrowser.gtdinboxPage;
				var loc = event.originalTarget.location;
				var newUrl = loc.href;
				var gtdinboxPage = null;
				if( tabGTD ){
					tabGTD.unload();
				}
				if( /^http(s?):\/\/mail.google.com/.test(newUrl) ){
					gtdinboxPage = new GTDInboxPage(gtdinboxBrowser, event.originalTarget);	// create new one:
					//gtdinboxBrowser.checkOptions();	// TODO DITCH THIS
				}
				}catch(e){if( GTDDEBUGMODE ) alert("fNewPage\n" + e.toString())}
			}
		};
	return fNewPage;
};




GTDInboxBrowser.prototype.getBrowserFromEvent = function(event){gBrowser.getBrowserForDocument(event.originalTarget);}; 



